﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ucntemaxp.DB;
using System.Linq.Expressions;

namespace ucntemaxp.Ctr
{
    class CtrForestilling
    {
        private DB.Dm74_1Entities DBCon;

        public CtrForestilling()
        {
            DBCon = new DB.Dm74_1Entities();
        }

        public Forestilling findForestilling(int id)
        {
            try
            {
                Forestilling f = DBCon.Forestillings.First(i => i.Forestillingid.Equals(id));
                return f;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public Forestilling findForestilling2(int filmid, DateTime dato)
        {
            try
            {
                Forestilling f = DBCon.Forestillings.First(i => i.Fid == filmid && i.Dato == dato);
                return f;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public List<Forestilling> findAlleForestilling()
        {
            List<Forestilling> forestilling = DBCon.Forestillings.Select(Forestilling => Forestilling).ToList();
            return forestilling;
        }

        public void opretSæder(int forestillingid, int salid)
        {
            var sæder = from x in DBCon.Sæde where x.Sædestatus.Equals("framevalgt") && x.Saleid==salid select x;
            foreach(Sæde s in sæder)
            {
                try
                {
                    Sæde s1 = new Sæde()
                    {
                        Plads = s.Plads,
                        Sædestatus = "fri",
                        Sale = DBCon.Sales.Single(c => c.Saleid == salid),
                        Bookingid = null,
                        Forestilling = DBCon.Forestillings.Single(c => c.Forestillingid == forestillingid)
                    };
                    DBCon.Sæde.AddObject(s1);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            DBCon.SaveChanges();
        }

        public List<Forestilling> findFilmForestillinger(int filmid, DateTime dato)
        {
            List<Forestilling> flist = new List<Forestilling>();
            try
            {
                var forestillinger = from x in DBCon.Forestillings where x.Fid == filmid && x.Dato.Value.Day == dato.Day && x.Dato.Value.Month == dato.Month && x.Dato.Value.Year == dato.Year select x;
                foreach(Forestilling f in forestillinger)
                {
                    flist.Add(f);
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            return flist;
        }

        public int selectMaxId()
        {
            try
            {
                var fore = (from x in DBCon.Forestillings select x.Forestillingid).Max();
                return fore;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public void addForestilling(string tidspunkt, DateTime dato, int tid, int fid, int saleid)
        {
            dato = new DateTime(
            dato.Ticks - (dato.Ticks % TimeSpan.TicksPerSecond),
            dato.Kind
            );

            try
            {
                DBCon.Forestillings.AddObject(new Forestilling()
                {
                    Tidspunkt = tidspunkt,
                    Dato = dato,
                    Tid = tid,
                    Fid = fid,
                    Saleid = saleid
                });
                DBCon.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void removeAllForestilling(string titel)
        {
            CtrFilm ctrf = new CtrFilm();
            List<Forestilling> flist = findAlleForestilling();
            foreach (Forestilling f in flist)
            {
                int forid1 = f.Fid;
                if (forid1 == ctrf.findFilm(titel).Filmid)
                {
                    int forid2 = forid1;
                    try
                    {
                        List<Sæde> sæder = DBCon.Sæde.Select(Sæde => Sæde).ToList();
                        foreach (Sæde s in sæder)
                        {
                            if (s.Forestillingid == f.Forestillingid)
                            {
                                DBCon.Sæde.DeleteObject(s);
                                DBCon.SaveChanges();
                            }
                        }
                        List<Booking> bookinger = DBCon.Bookings.Select(Booking => Booking).ToList();
                        foreach (Booking b in bookinger)
                        {
                            if (b.Fid == f.Forestillingid)
                            {
                                DBCon.Bookings.DeleteObject(b);
                                DBCon.SaveChanges();
                            }
                        }
                        Forestilling for2 = DBCon.Forestillings.First(r => r.Forestillingid == f.Forestillingid);
                        DBCon.Forestillings.DeleteObject(for2);
                        DBCon.SaveChanges();
                    }
                    catch (Exception ex)
                    {

                        throw ex;
                    }
                }
            }
        }

        public void removeForestilling(int forestillingid)
        {
            try
            {
                List<Sæde> sæder = DBCon.Sæde.Select(Sæde => Sæde).ToList();
                foreach(Sæde s in sæder)
                {
                    if(s.Forestillingid == forestillingid)
                    {
                        DBCon.Sæde.DeleteObject(s);
                        DBCon.SaveChanges();
                    }
                }
                List<Booking> bookinger = DBCon.Bookings.Select(Booking => Booking).ToList();
                foreach (Booking b in bookinger)
                {
                    if (b.Fid == forestillingid)
                    {
                        DBCon.Bookings.DeleteObject(b);
                        DBCon.SaveChanges();
                    }
                }
                Forestilling f = DBCon.Forestillings.First(r => r.Forestillingid == forestillingid);
                DBCon.Forestillings.DeleteObject(f);
                DBCon.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void updateForestilling(int forestillingid, string tidspunkt, DateTime dato, int tid, int fid, int saleid)
        {
            try
            {
                Forestilling f = DBCon.Forestillings.First(i => i.Forestillingid == forestillingid);
                f.Tidspunkt = tidspunkt;
                f.Dato = dato;
                f.Tid = tid;
                f.Fid = fid;
                f.Saleid = saleid;
                DBCon.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void Update(Action<Forestilling> updateStatement, Expression<Func<Forestilling, bool>> where)
        {
            // get your object context & objectset, cast to IQueryable<T>
            var table = (IQueryable<Forestilling>)DBCon.CreateObjectSet<Forestilling>();

            // filter with the Expression
            var items = table.Where(where);

            // perform the Action on each item
            foreach (var item in items)
            {
                updateStatement(item);
            }

            // save changes.
        }
    }
}
